﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Web.UI.WebControls;
using System.Data;
using System.Web.UI.HtmlControls;

namespace DCTLib.Common
{
    public class ControlLoader
    {
        //Class giúp load dữ liệu vào các Control

        public static void LoadDropDownList(DropDownList pCtrl, DataTable dataSource, string pstrValueField, string pstrTextField, ListItem blankItem)
        {
            pCtrl.Items.Clear();
            pCtrl.DataSource = dataSource;
            pCtrl.DataValueField = pstrValueField;
            pCtrl.DataTextField = pstrTextField;
            pCtrl.DataBind();

            if (blankItem != null)
            {
                pCtrl.Items.Insert(0, blankItem);
            }
        }

        public static void LoadDropDownList(ListBox ltb, DataTable dataSource, string pstrValueField, string pstrTextField, ListItem blankItem)
        {
            ltb.Items.Clear();
            ltb.DataSource = dataSource;
            ltb.DataValueField = pstrValueField;
            ltb.DataTextField = pstrTextField;
            ltb.DataBind();

            if (blankItem != null)
            {
                ltb.Items.Insert(0, blankItem);
            }
        }

        public static void LoadDropDownList(DropDownList pCtrl, DataTable dataSource, string pstrValueField, string pstrTextField)
        {
            LoadDropDownList(pCtrl, dataSource, pstrValueField, pstrTextField, null);
        }

        public static void LoadDropDownList(HtmlSelect pCtrl, DataTable dataSource, string pstrValueField, string pstrTextField, ListItem blankItem)
        {
            pCtrl.Items.Clear();
            pCtrl.DataSource = dataSource;

            pCtrl.DataValueField = pstrValueField;
            pCtrl.DataTextField = pstrTextField;
            pCtrl.DataBind();

            if (blankItem != null)
            {
                pCtrl.Items.Insert(0, blankItem);
            }
        }

        public static void LoadDropDownList(HtmlSelect pCtrl, DataTable dataSource, string pstrValueField, string pstrTextField)
        {
            LoadDropDownList(pCtrl, dataSource, pstrValueField, pstrTextField, null);
        }

        /// <summary>
        /// Bind a datagrid with specified datasource.
        /// </summary>
        /// <param name="dtg">The datagrid for binding</param>
        /// <param name="dataSource">Datasource for the grid</param>
        /// <param name="PageIndex">Page index</param>
        public static void BindDatagrid(DataGrid dtg, DataTable dataSource, int PageIndex)
        {
            dtg.DataSource = dataSource;
            dtg.CurrentPageIndex = PageIndex;
            dtg.DataBind();
        }

        public static void BindDatagrid(GridView grv, DataTable dataSource, int PageIndex)
        {
            grv.DataSource = dataSource;
            grv.PageIndex = PageIndex;
            grv.DataBind();
        }

        public static void BindDatagrid(Repeater rpt, DataTable dataSource)
        {
            rpt.DataSource = dataSource;
            rpt.DataBind();
        } 
    }
}
